package com.mhkj.base.rbac.permission.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import com.mhkj.base.rbac.permission.dao.PermissionDao;
import com.mhkj.base.rbac.permission.entity.Permission;
import com.mhkj.base.rbac.permission.service.PermissionService;
import com.mics.core.common.componet.Pagination;
import com.mics.core.system.base.service.impl.BaseServiceImpl;
import com.mics.core.system.rbac.service.DictionaryService;

@Service(value="permission2")
public class PermissionServiceImpl extends BaseServiceImpl<Permission> implements PermissionService {
	@Resource
	private PermissionDao permissionDao;
	@Resource
	private DictionaryService dictionaryService;

	@Resource
	public void setPermissionDao(PermissionDao PermissionDao) {
		super.setBaseRepository(PermissionDao);
	}

	@Override
	public Pagination<Permission> listByPage(Permission Permission, int pageIndex, int size ) {
		Specification<Permission> spec = new Specification<Permission>() {
			/**
			 * 序列号
			 */
			private static final long serialVersionUID = -786003485941679959L;

			@Override
			public Predicate toPredicate(Root<Permission> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
				List<Predicate> lstPredicates = new ArrayList<Predicate>();
				Predicate[] arrayPredicates = new Predicate[lstPredicates.size()];
				

//				 Join<Object, Object> tSysNation = root.join("tSysNation",JoinType.INNER);
//
//				if (!StringUtils.isEmpty(Permission.getCityId())) {
//					lstPredicates.add(criteriaBuilder.equal(tSysNation.get("id"),Permission.getCityId()));
//				}
//				
//				if (!StringUtils.isEmpty(Permission.getProvinceId())) {
//					lstPredicates.add(criteriaBuilder.equal(tSysNation.get("parentId"),Permission.getProvinceId()));
//				}
//				
//				if (!StringUtils.isEmpty(Permission.getName())) {
//					lstPredicates.add(criteriaBuilder.like(root.get("name").as(String.class), "%" + Permission.getName() + "%"));
//				}
//				 
//				if (!StringUtils.isEmpty(Permission.getAffiliatedCompanyId())) {
//					lstPredicates.add(criteriaBuilder.equal(root.get("affiliatedCompanyId").as(String.class), Permission.getAffiliatedCompanyId()));
//				}

			 
				return criteriaBuilder.and(lstPredicates.toArray(arrayPredicates));
			}
		};
		Page<Permission> pageT = permissionDao.findAll(spec, PageRequest.of(pageIndex, size, Sort.Direction.ASC, "id"));
		Pagination<Permission> pagination = new Pagination<Permission>();
		pagination.setTotal(permissionDao.count(spec));
		pagination.setData(pageT.getContent());
		return pagination;
	}
 
	}


